package org.btik.light.server.platform.event;

import org.btik.context.AppContext;
import org.btik.context.event.SystemStartHook;
import org.btik.light.server.platform.common.api.bean.settings.SystemSettings;
import org.btik.light.server.platform.common.api.bean.user.User;
import org.btik.light.server.platform.common.api.dao.LightDao;
import org.btik.light.tool.MDUtil;
import org.btik.light.server.platform.common.api.bean.dev.access.DevAccount;


import java.util.Date;
import java.util.Map;

import static org.btik.light.server.platform.common.api.service.SystemSettingsService.SYSTEM_SETTINGS;
import static org.btik.light.tool.MDUtil.Algorithm.MD5;
import static org.btik.light.tool.MDUtil.Algorithm.SHA_512_256;


public class DefaultProperties implements SystemStartHook {

    private String defaultDevAccount;

    private String defaultDevPassword;

    private String defaultUserAccount;

    private String defaultUserPassword;
    private String defaultUserRemark;

    public Map<String, String> defaultSettings;


    @Override
    public void onSystemStart(AppContext appContext) {
        LightDao lightDao = appContext.getBean(LightDao.class);
        DevAccount devAccount = lightDao.get(defaultDevAccount, DevAccount.class);
        if (null == devAccount) {
            DevAccount newDevAccount = new DevAccount();
            newDevAccount.setUsername(defaultDevAccount);
            newDevAccount.setPasswd(MDUtil
                    .digestAsHex(MD5, defaultDevAccount + defaultDevPassword));
            newDevAccount.setMdAlgorithm(MD5);
            lightDao.save(newDevAccount);
        }
        User user = lightDao.get(defaultUserAccount, User.class);
        if (null == user) {
            user = new User();
            Date now = new Date();
            user.setAccount(defaultUserAccount);
            user.setUsername(defaultUserAccount);
            user.setCreatTime(now);
            user.setUpdateTime(now);
            user.setEnable(true);
            user.setAdmin(true);
            user.setRemark(defaultUserRemark);
            user.setPassword(MDUtil
                    .digestAsHex(SHA_512_256, defaultUserAccount + defaultUserPassword));
            lightDao.save(user);
        }

        SystemSettings systemSettings = lightDao.get(SYSTEM_SETTINGS, SystemSettings.class);
        if (systemSettings == null) {
            systemSettings = new SystemSettings();
            systemSettings.setValues(defaultSettings);
            lightDao.save(systemSettings);
        }


    }

    public void setDefaultDevPassword(String defaultDevPassword) {
        this.defaultDevPassword = defaultDevPassword;
    }

    public void setDefaultDevAccount(String defaultDevAccount) {
        this.defaultDevAccount = defaultDevAccount;
    }

    public void setDefaultUserAccount(String defaultUserAccount) {
        this.defaultUserAccount = defaultUserAccount;
    }

    public void setDefaultUserPassword(String defaultUserPassword) {
        this.defaultUserPassword = defaultUserPassword;
    }

    public void setDefaultUserRemark(String defaultUserRemark) {
        this.defaultUserRemark = defaultUserRemark;
    }


    public void setDefaultSettings(Map<String, String> defaultSettings) {
        this.defaultSettings = defaultSettings;
    }
}
